package com.ming.sorting.algorithm;

import java.util.Arrays;

/*
冒泡排序
 */
public class BubbleSort {
    public static void main(String[] args) {
        int[] arr = {3, -2, 9, -1, 10};
        long l = System.currentTimeMillis();
        bubbleSort(arr);
        long l1 = System.currentTimeMillis();
        System.out.println(l1-l);
        System.out.println(Arrays.toString(arr));
    }

    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            boolean flag = false;
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    flag = true;
                    arr[j] ^= arr[j + 1];
                    arr[j + 1] ^= arr[j];
                    arr[j] ^= arr[j + 1];
                }
            }
            System.out.println("第" + (i + 1) + "趟排序后的数组");
            System.out.println(Arrays.toString(arr));
            if (!flag) break;
        }
    }
}
